package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.ZyBuilding;
import com.blb.zy_community.entity.ZyRoom;
import com.blb.zy_community.entity.ZyUnit;
import com.blb.zy_community.mapper.ZyRoomMapper;
import com.blb.zy_community.service.impl.ZyBuildingServiceImpl;
import com.blb.zy_community.service.impl.ZyRoomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房间  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class ZyRoomController {

    @Autowired
    private ZyRoomServiceImpl zyRoomService;




    @RequestMapping("/roomList")
    public ResponseResult<ZyRoom> getZyRoomList(@RequestParam(required = false, defaultValue = "1") Long current,
                                                @RequestParam(required = false, defaultValue = "5") Long size,
                                                @RequestParam(required = false) String unitId,
                                                @RequestParam(required = false) String buildingId,
                                                @RequestParam(required = false) String roomStatus,
                                                @RequestParam(required = false) Long communityId){


        Page<ZyRoom> zyRoomPage = zyRoomService.selectRoomList(new Page<>(current,size), unitId, buildingId, roomStatus,communityId);
        return ResponseResult.ok(zyRoomPage);

    }

    /**
     * 删除
     * @param roomId
     * @return
     */
    @DeleteMapping("/roomDelete/{roomId}")
    public ResponseResult<String> deleteRoomId(@PathVariable Long roomId){
        zyRoomService.removeById(roomId);
        return ResponseResult.ok(roomId);
    }

    /**
     * 查询楼栋-单元级联
     * @param
     * @return
     */
    @GetMapping("/children")
    public ResponseResult<List<ZyRoom>> getUnitBuildingChildren(){
        List<ZyBuilding> zyBuildingList = zyRoomService.getbuildingList();
        return ResponseResult.ok(zyBuildingList);
    }


    /**
     * 添加
     */
    @PostMapping("/roomSave")
    public ResponseResult<String> saveRoom(@RequestBody ZyRoom zyRoom){
        zyRoom.setCreateTime(LocalDateTime.now());
        zyRoomService.save(zyRoom);
        return ResponseResult.ok(zyRoom);
    }

    @PutMapping("/roomUpdate")
    public ResponseResult<String> updateRoom(@RequestBody ZyRoom zyRoom){
        zyRoomService.updateById(zyRoom);
        return ResponseResult.ok(zyRoom);
    }

    @GetMapping("unitByRoom")
    public ResponseResult<String> unitByRoomById(@RequestParam Long id){
        List<ZyRoom> unitId = zyRoomService.list(new QueryWrapper<ZyRoom>().eq("unit_id", id));
        return ResponseResult.ok(unitId);
    }

    /**
     * 批量删除--根据房屋id
     * @param roomIds
     * @return
     */
    @DeleteMapping("/room/{ids}")
    public ResponseResult<String> batchDeleteIds(@PathVariable("ids") Long[] roomIds){
        zyRoomService.batchDeleteIds(roomIds);
        return ResponseResult.ok(roomIds);
    }

}
